/* Copyright (c) 2009 SpringShowcase, Inc.
 * All right reserved.
 * http://code.google.com/p/springshowcase/
 * This software is the confidential and proprietary information of BeyondWeb
 * , Inc. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with BeyondWeb.
 *
 * Revision History
 * Author              Date             Description
 * ------------------   --------------    ------------------
 *  kwondohee           2010. 4. 2.        
 */
package org.springshowcase.mvc;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springshowcase.core.commons.config.Configuration;
import org.springshowcase.core.mvc.JsonView;

/**
 * The Class FormTypeEditController.
 * 
 * @author kwondohee
 */
@Controller
@SessionAttributes("user")
@RequestMapping("/form/ajaxAddForm")
public class AjaxTypeEditController {
	// ---------------------------------------------------------------------
	// Class Variable Declaration
	// ---------------------------------------------------------------------
	/** The Constant logger. */
	private static final Logger logger = LoggerFactory
			.getLogger(AjaxTypeEditController.class);

	@Autowired
	private FormTypeFacade formTypeFacade;

	@Autowired
	private Configuration configuration;

	// ---------------------------------------------------------------------
	// Constructor Method Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// DI Method (setter,getter) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// this Method (public/private) Declaration
	// ---------------------------------------------------------------------

	/**
	 * HTTP Get 요청시 처리.
	 * 
	 * @param id the id
	 * @param model the model
	 * @return the model and view
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView setupForm(
			@RequestParam(value = "id", required = false) String id,
			ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView();
		// 반드시 초기값 설정을 해야 한다.
		User user = new User();
		// 추가된 객체는 @SessionAttributes("user")으로 설정 된다.
		// HttpSession.getAttribute("user") 동일 하다.
		model.addAttribute("user", user);
		return mav;
	}

	/**
	 * HTTP Post요청시 처리 (Process submit이 발생되었을 경우.)
	 * 
	 * @param pet the pet
	 * @param result the result
	 * @param status the status
	 * @return the string
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView processSubmit(@Valid @ModelAttribute("user") User user,
			BindingResult result, SessionStatus status, ModelMap model,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();
		if (result.hasErrors()) {
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			mav.addObject(result);
		}
		else {
			formTypeFacade.executeAddForm(user);
			model.addAttribute(JsonView.CONTENT_OBJECT, user);
		}
		return mav;
	}

	// ---------------------------------------------------------------------
	// interface implement Method (public/private) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// abstract implement Method (public/private) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// abstract Method (public/private) Declaration
	// ---------------------------------------------------------------------
}
